//
//  RechargeViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/3/12.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "RechargeViewController.h"
#import "RechargeTFView.h"
#import "PaySuccViewController.h"
#import "ZJDefines.h"
#import "ZJPayPopupView.h"
#import "ConFirmPayView.h"
#import "AddDepositCardViewController.h"

#import <MMNumberKeyboard.h>


@interface RechargeViewController ()<ZJPayPopupViewDelegate,MMNumberKeyboardDelegate>
{
    NSArray *bankList;
}
@property (nonatomic, strong) ZJPayPopupView *payPopupView;
@property (nonatomic, strong) RechargeTFView *rechargeTF;

@property (nonatomic, strong) ConFirmPayView *comfirmPayView;
@end

@implementation RechargeViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"充值";
    self.view.backgroundColor = WB_RGBColor(251, 251, 251);
    [self setupUI];
    [self getBank];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
 //   [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)getBank
{
    [XYHttpRequest dataRequestWithMyBankCardListType:0 success:^(id responseObj) {
        
        if (Success) {
            bankList = [NSArray array];
            bankList = responseObj[@"data"][@"result"];
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)setupUI
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight - kNavigationBarHeight + 1);
    scrollView.bounces = YES;
    scrollView.backgroundColor = WB_RGBColor(240, 240, 240);
    [self.view addSubview:scrollView];
    
    MMNumberKeyboard *keyboard = [[MMNumberKeyboard alloc] initWithFrame:CGRectZero];
    keyboard.allowsDecimalPoint = YES;
    keyboard.delegate = self;
    
    [scrollView addSubview:self.rechargeTF];
    self.rechargeTF.moneyTF.inputView = keyboard;
    [_rechargeTF.moneyTF becomeFirstResponder];
}

- (void)click:(UIButton *)sender
{
    if (WB_IsNilString(self.rechargeTF.moneyTF.text)) {
        [NSObject wb_showAlertWithTitle:@"请输入金额" duration:1];
        return;
    }
    [_rechargeTF.moneyTF resignFirstResponder];
    
    [XYHttpRequest dataRequestWithDefaultBankCardsuccess:^(id responseObj) {
        
        if (Success) {
            
            NSArray *arr = responseObj[@"data"][@"result"];
            
            if (!arr.count) {
                
                [NSObject wb_showAlertWithTitle:@"暂未绑卡" duration:1];
                return;
            }
            NSDictionary* dic = arr[0];
            self.comfirmPayView.nameLb.text = dic[@"cardCustName"];
            [self.comfirmPayView.bankInfoLb setTitle:dic[@"desCardName"] forState:UIControlStateNormal];
            [self.comfirmPayView show];
            self.comfirmPayView.bankData = bankList;
        }
        
    } failure:^(NSError *error) {
        
    }];
    self.comfirmPayView.payMoneyLb.text = [NSString stringWithFormat:@"￥%@",self.rechargeTF.moneyTF.text];
    

}

- (BOOL)numberKeyboard:(MMNumberKeyboard *)numberKeyboard shouldInsertText:(NSString *)text
{
    BOOL hasInputDot = NO;
    if (self.rechargeTF.moneyTF.text.length == 0) {
        return ![text isEqualToString:@"."];
    } else {
        if ([self.rechargeTF.moneyTF.text rangeOfString:@"."].location == NSNotFound) {
            if ([text isEqualToString:@"."]) {
                hasInputDot = YES;
                return YES;
            }
        } else {
            if ([text isEqualToString:@"."]) {
                return NO;
            } else {
                NSRange ran = [self.rechargeTF.moneyTF.text rangeOfString:@"."];
                return !(self.rechargeTF.moneyTF.text.length - ran.location > 2);
            }
        }
    }
    return YES;
}


#pragma mark - ZJPayPopupViewDelegate

- (void)didClickForgetPasswordButton
{
    NSLog(@"点击了忘记密码");
}

- (void)didPasswordInputFinished:(NSString *)password
{
    WB_WeakSelf(self)
    [XYHttpRequest dataRequestWithCheckTraderOldPwd:password success:^(id responseObj) {
        if (Success) {
            //隐藏键盘
            [weakself.payPopupView hidePayPopView];
            [XYHttpRequest dataRequestWithRechargeMoney:_rechargeTF.moneyTF.text success:^(id responseObj) {
                if (Success) {
                    PaySuccViewController *vc = [[PaySuccViewController alloc] init];
                    vc.moneyNum = self.rechargeTF.moneyTF.text;
                    
                    [weakself.navigationController pushViewController:vc animated:YES];
                }else{
                    [NSObject wb_showAlertWithTitle:responseObj[@"info"] duration:1];
                }
            } failure:^(NSError *error) {
            }];  
        }else{
            
            [NSObject wb_showAlertWithTitle:responseObj[@"info"]];
        }
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 懒加载  Lazy Load
- (RechargeTFView *)rechargeTF
{
    if (_rechargeTF == nil) {
        _rechargeTF = [[NSBundle mainBundle] loadNibNamed:@"RechargeTFView" owner:self options:nil].firstObject;
        _rechargeTF.frame = CGRectMake(15, 25, kScreenWidth - 30, 250);
        [_rechargeTF.rechargeBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rechargeTF;
}
- (ConFirmPayView *)comfirmPayView
{
    if (!_comfirmPayView) {
        
        _comfirmPayView = [[NSBundle mainBundle]loadNibNamed:@"ConFirmPayView" owner:nil options:nil].firstObject;
        
        WB_WEAKSELF;
        _comfirmPayView.addCard = ^{
          
            AddDepositCardViewController *vc = [[AddDepositCardViewController alloc] init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        _comfirmPayView.payClick = ^{
                weakSelf.payPopupView = [[ZJPayPopupView alloc] init];
                weakSelf.payPopupView.delegate = weakSelf;
                [weakSelf.payPopupView showPayPopView];
        };
    }
    return _comfirmPayView;
}
@end
